import React, { useState, useEffect } from 'react'
import { InputNumber } from 'antd'

const formatMoneyJson = {
  formatter: value => `$ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
  parser: value => value.replace(/\$\s?|(,*)/g, '')
}

function Main ({ value, onChange, config = {} }) {
  const [format, setFormat] = useState({})
  const [c, setC] = useState({})
  useEffect(() => {
    if (config) {
      if (config.isMoney) {
        setFormat(formatMoneyJson)
        delete config.isMoney
      }
    }
    setC(config)
  }, [config])
  return (
    <InputNumber style={{ width: '100%' }} placeholder='请输入' value={value} onChange={onChange} {...c} {...format} />
  )
}

export default Main
